package com.kcsj.util;


import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;

public class GenerateTaskListNo {
    public static String generate() throws IOException {
        String part1 = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)-2000);
        String part2 = "";
        String part3= No();
        if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR)/10==0){
            part2 = "00"+Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        }else if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR)/10>0&&
                Calendar.getInstance().get(Calendar.DAY_OF_YEAR)/10<10){
            part2 = "0"+Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        }else if(Calendar.getInstance().get(Calendar.DAY_OF_YEAR)/10>=10){
            part2 = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
        }
        return part1+part2+part3;
    }
    private static String No() throws IOException {
        //创建Properties集合
        Properties prop = new Properties();
        //判断配置文件是否存在
        File f = new File("count.properties");
        if(!f.exists())
            f.createNewFile();
        //创建字符读取流对象
        FileReader fr = new FileReader(f);
        //将配置文件信息加载到Properties集合中
        prop.load(fr);
        //获取计数次数
        int count = 0;
        int time = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        String value = prop.getProperty("count");
        //判断是否是null
        if(value != null){
            count = Integer.parseInt(value);
        }
        if (Integer.parseInt(prop.getProperty("time")) != time){
            count = 1;
            value = 1 + "";
        }


        //修改prop集合中time对应的值
        prop.setProperty("time", time+"");
        count++;
        //修改prop集合中count对应的值
        prop.setProperty("count", count+"");

        //创建字符写入流对象
        FileWriter fw = new FileWriter(f);
        //将prop集合中键值对存到配置文件中
        prop.store(fw, "");
        //关闭资源
        fr.close();
        fw.close();
        if (Integer.parseInt(value)/10==0){
            return "00"+value;
        }else if (Integer.parseInt(value)/10>0&&
                Integer.parseInt(value)/10<10){
            return "0" +value;
        }
        return value;
    }
}
